package com.supor.aiot.basket.adapter;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baseconfig.common.views.RoundImageView;
import com.bumptech.glide.Glide;
import com.supor.aiot.basket.R;
import com.supor.aiot.basket.bean.IngredientBean;
import com.supor.aiot.basket.bean.IsMainIngredients;
import com.supor.aiot.basket.view.DoubleClickUtils;

import java.util.List;


public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketHolder>{
    private List<IngredientBean> mList;
    private Context mContext;
    private OnBasketItemListener listener;
    private IngredientSelectedListener ingredientSelectedListener;
    private boolean managerState;
    public BasketAdapter(List<IngredientBean> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    public void setBasketListener(OnBasketItemListener listener){
        this.listener = listener;
    }

    public void setIngredientSelectedListener(IngredientSelectedListener listener){
        this.ingredientSelectedListener = listener;
    }

    public void setCBallState(boolean managerState){
        this.managerState=managerState;
    }

    public void setCBallState(boolean managerState,boolean cbstate){
        this.managerState=managerState;
        if (managerState){
            for (int i = 0;i<mList.size();i++){
                mList.get(i).setSelected(cbstate);
            }
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public BasketAdapter.BasketHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_basket_recipe,parent, false);
        return new BasketAdapter.BasketHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(@NonNull BasketHolder holder, int position) {
        IngredientBean bean = mList.get(position);
        if (managerState){
            holder.cbForAll.setChecked(bean.isSelected());
        }else {
            if (bean.getIsMainIngredients().size()>0){
                boolean checked = true;
                for (IsMainIngredients ingredients:bean.getIsMainIngredients()){
                    if (ingredients.getIsSelect()==0)checked=false;
                }
                if (bean.getIsTsIngredients().size()>0){
                    for (IsMainIngredients ingredients:bean.getIsTsIngredients()){
                        if (ingredients.getIsSelect()==0)checked=false;
                    }
                }
                holder.cbForAll.setChecked(checked);
            }
        }

        holder.tvBasketTitle.setText(bean.getRecipeName());
        Glide.with(mContext).load(bean.getSmallLandscape()).into(holder.ivRecipe);
        BasketOuterAdapter adapter = new BasketOuterAdapter(bean,mContext);
        holder.rvHideDetail.setAdapter(adapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        holder.rvHideDetail.setLayoutManager(linearLayoutManager);

        if (bean.getIsMainIngredients()!=null&&bean.getIsMainIngredients().size()>0){
            MainIngredientAdapter mainIngredientAdapter = new MainIngredientAdapter(bean.getIsMainIngredients(),mContext,ingredientSelectedListener);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            manager.setOrientation(RecyclerView.VERTICAL);
            holder.rvMainIngredient.setLayoutManager(manager);
            holder.rvMainIngredient.setAdapter(mainIngredientAdapter);
            holder.llMainOuter.setVisibility(View.VISIBLE);
        }else {
            holder.llMainOuter.setVisibility(View.GONE);
        }

        if (bean.getIsTsIngredients()!=null&&bean.getIsTsIngredients().size()>0){
            OtherIngredientAdapter otherIngredientAdapter = new OtherIngredientAdapter(bean.getIsTsIngredients(),mContext,ingredientSelectedListener);
            holder.rvOtherIngredient.setAdapter(otherIngredientAdapter);
            LinearLayoutManager manager1 = new LinearLayoutManager(mContext);
            manager1.setOrientation(RecyclerView.VERTICAL);
            holder.rvOtherIngredient.setLayoutManager(manager1);
            holder.llOtherOuter.setVisibility(View.VISIBLE);
        }else {
            holder.llOtherOuter.setVisibility(View.GONE);
        }

        if (bean.getIsCgIngredients()!=null&&bean.getIsCgIngredients().size()>0){
            CommonIngredientAdapter commonIngredientAdapter = new CommonIngredientAdapter(bean.getIsCgIngredients(),mContext,ingredientSelectedListener);
            holder.rvCommonIngredient.setAdapter(commonIngredientAdapter);
            LinearLayoutManager manager2 = new LinearLayoutManager(mContext);
            manager2.setOrientation(RecyclerView.HORIZONTAL);
            holder.rvCommonIngredient.setLayoutManager(manager2);
            holder.rvCommonIngredient.setBackgroundColor(mContext.getColor(R.color.white));
            holder.llCommonOuter.setVisibility(View.VISIBLE);
        }else {
            holder.llCommonOuter.setVisibility(View.GONE);
        }

        holder.cbClick.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b){
                holder.rvHideDetail.setVisibility(View.GONE);
                holder.llShowDetail.setVisibility(View.VISIBLE);
            }else {
                holder.llShowDetail.setVisibility(View.GONE);
                holder.rvHideDetail.setVisibility(View.VISIBLE);
            }
        });

        holder.cbForAll.setOnClickListener(view -> {listener.onCheckBox(position,holder.cbForAll.isChecked());});
        holder.tvBasketDetail.setOnClickListener(view -> {
            if (DoubleClickUtils.isFastDoubleClick())return;
            listener.onCheckDetail(position);
        });

    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class BasketHolder extends RecyclerView.ViewHolder{
        private CheckBox cbForAll;
        private CheckBox cbClick;
        private TextView tvBasketTitle;
        private TextView tvBasketDetail;
        private LinearLayout llShowDetail;
        private RecyclerView rvHideDetail;
        private RecyclerView rvMainIngredient;
        private RecyclerView rvOtherIngredient;
        private RecyclerView rvCommonIngredient;
        private RoundImageView ivRecipe;
        private LinearLayout llOtherOuter;
        private LinearLayout llCommonOuter;
        private LinearLayout llMainOuter;

        public BasketHolder(@NonNull View itemView) {
            super(itemView);
            cbForAll = itemView.findViewById(R.id.cb_for_all);
            cbClick = itemView.findViewById(R.id.cb_click_up);
            ivRecipe = itemView.findViewById(R.id.iv_basket_img);
            tvBasketTitle = itemView.findViewById(R.id.tv_basket_title);
            tvBasketDetail = itemView.findViewById(R.id.tv_detail);
            llShowDetail = itemView.findViewById(R.id.ll_show_detail);
            rvHideDetail = itemView.findViewById(R.id.rv_hide_detail);
            rvMainIngredient = itemView.findViewById(R.id.rv_main_ingredients);
            rvOtherIngredient = itemView.findViewById(R.id.rv_other_ingredients);
            rvCommonIngredient = itemView.findViewById(R.id.rv_common_ingredients);
            llOtherOuter = itemView.findViewById(R.id.ll_other_outer);
            llCommonOuter = itemView.findViewById(R.id.ll_common_outer);
            llMainOuter = itemView.findViewById(R.id.ll_main_outer);
        }
    }

    public interface OnBasketItemListener{
        void onCheckBox(int position, boolean state);
        void onCheckDetail(int position);
    }

}
